package com.example.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Project:spring-boot-rabbitmq
 * <p>
 * Title:RabbitMqConfig.java
 * <p>
 * Description:
 * <p>
 * Copyrigth:Jiang Software LTD.co Copyright (c) 2021
 * <p>
 * 
 * @version 1.0
 * @since 1.8
 * @author jiang
 * @history 2021 jiang create
 */

@Configuration
public class RabbitMqConfig {

	public static final String EXCHANGE = "rabbitmq.exchange"; // 交换空间名称
	public static final String ROUTINGKEY = "rabbitmq.routingkey"; // 设置路由key
	public static final String QUEUE_NAME = "rabbitmq.queue"; // 设置队列名称

	/**
	 * 根据路由键将队列和交换机绑定到一起
	 *
	 * @param exchange
	 * @param queue
	 * @return
	 */
	@Bean
	public Binding bindingExchangeQueue(DirectExchange exchange, Queue queue) {
		Binding binding = BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY);
		return binding;
	}

	/**
	 * 使用直连的模式
	 *
	 * @return
	 */
	@Bean
	public DirectExchange getDirectExchage() {
		return new DirectExchange(EXCHANGE, true, true);
	}

	/**
	 * 队列消息
	 *
	 * @return
	 */
	@Bean
	public Queue queue() {
		return new Queue(QUEUE_NAME);
	}

}
